package db.exchange.client.clientgui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import db.exchange.client.Client;

public class MainWindow extends JFrame {

	ControllerGUI controller;

	private static final long serialVersionUID = 1L;
	private JTextField textField_clientId;
	private JTextField textField_symbol;
	private JTextField textField_qty;
	private JTextField textField_price;
	JTextPane textPane_allOrders;
	JLabel lblHelp;
	private JTextField textField_host;
	private JTextField textField_port;

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// MainWindow frame = new MainWindow();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	public static void startClientGUI() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					// Client.mainwindow = frame;
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MainWindow() {
		setTitle("Client");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 580, 506);
		getContentPane().setLayout(null);

		textField_clientId = new JTextField();
		final JButton btnConnect = new JButton("connect");
		textField_symbol = new JTextField();
		final JButton btnSubmit = new JButton("Submit");
		final JLabel lblSymbol = new JLabel("Symbol");
		textField_qty = new JTextField();
		final JLabel lblQuantity = new JLabel("Quantity");
		textField_price = new JTextField();
		final JLabel lblPrice = new JLabel("Price");
		textPane_allOrders = new JTextPane();// ////////////////////
		final JLabel lblClientid = new JLabel("ClientID:");
		lblHelp = new JLabel("please connect");
		JLabel lblBuySell = new JLabel("Buy/Sell");
		JLabel lblHost = new JLabel("Host:");
		JLabel lblPort = new JLabel("Port:");
		JButton btnLogin = new JButton("login");
		textField_port = new JTextField();
		textField_host = new JTextField("localhost");

		lblHelp.setForeground(Color.BLUE);

		textField_clientId.setBounds(339, 47, 86, 20);
		getContentPane().add(textField_clientId);
		textField_clientId.setColumns(10);

		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String host = textField_host.getText();
				String port = textField_port.getText();
				if ("".equals(host)) {
					lblHelp.setForeground(Color.RED);
					lblHelp.setText("Connection failed: host is missed");
					return;
				} else if ("".equals(port)) {
					lblHelp.setForeground(Color.RED);
					lblHelp.setText("Connection port: host is missed");
					return;
				}
				{
					lblHelp.setText("Do connection ... ");
					Client.doConnect(host, port);
				}
			}
		});
		btnConnect.setBounds(437, 13, 105, 20);
		getContentPane().add(btnConnect);

		final JComboBox<String> comboBoxBuySell = new JComboBox<>();
		comboBoxBuySell.setBounds(125, 90, 98, 20);
		getContentPane().add(comboBoxBuySell);
		comboBoxBuySell.addItem("BUY");
		comboBoxBuySell.addItem("SELL");

		textField_symbol.setBounds(27, 90, 86, 20);
		getContentPane().add(textField_symbol);
		textField_symbol.setColumns(10);

		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String orderStatusMessage = controller.constructNewOrder(
						textField_clientId.getText(), null,
						textField_symbol.getText(),
						(String) comboBoxBuySell.getSelectedItem(),
						textField_price.getText(), textField_qty.getText());
				lblHelp.setText(orderStatusMessage);
			}
		});
		btnSubmit.setBounds(441, 89, 75, 23);
		getContentPane().add(btnSubmit);

		lblSymbol.setBounds(27, 75, 46, 14);
		getContentPane().add(lblSymbol);

		textField_qty.setBounds(245, 90, 86, 20);
		getContentPane().add(textField_qty);
		textField_qty.setColumns(10);

		lblQuantity.setBounds(245, 75, 67, 14);
		getContentPane().add(lblQuantity);

		textField_price.setBounds(343, 90, 86, 20);
		getContentPane().add(textField_price);
		textField_price.setColumns(10);

		lblPrice.setBounds(343, 75, 46, 14);
		getContentPane().add(lblPrice);

		textPane_allOrders.setBounds(27, 150, 515, 284);
		getContentPane().add(textPane_allOrders);

		lblClientid.setBounds(237, 50, 75, 14);
		getContentPane().add(lblClientid);

		lblHelp.setBounds(30, 125, 512, 14);
		getContentPane().add(lblHelp);

		lblBuySell.setBounds(125, 74, 56, 16);
		getContentPane().add(lblBuySell);

		btnLogin.setBounds(437, 47, 105, 20);
		getContentPane().add(btnLogin);

		lblHost.setBounds(27, 16, 56, 16);
		getContentPane().add(lblHost);

		textField_host.setBounds(75, 14, 140, 22);
		getContentPane().add(textField_host);
		textField_host.setColumns(10);

		textField_port.setText("2013");
		textField_port.setBounds(310, 13, 116, 22);
		getContentPane().add(textField_port);
		textField_port.setColumns(10);

		lblPort.setBounds(262, 16, 36, 16);
		getContentPane().add(lblPort);

	}
}
